package com.fantuan.db.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Service;

import com.fantuan.db.bean.FundLog;

@Service
public class FundLogDAO extends BaseDAO{

	/**
	 * 获取某一条资金记录
	 * @param nid
	 * @return
	 * @throws SQLException
	 */
	public FundLog getFundLog(int nid) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from t_yw_ftfundlog where nid=?";

		FundLog result = null;
		try {

			Object[] objs = new Object[] { nid };

			result = (FundLog) this.query(conn, sql, new BeanHandler<FundLog>(
					FundLog.class), objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
	
	/**
	 * 获取某用户的资金日志列表
	 * @param userid
	 * @return
	 * @throws SQLException
	 */
	public List<FundLog> getFundLogListByUserID(int userid) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from t_yw_ftfundlog where nUserID=? order by nid desc";

		List<FundLog> result = null;
		try {

			Object[] objs = new Object[] { userid };

			result = (List<FundLog>) this.query(conn, sql, new BeanListHandler<FundLog>(
					FundLog.class), objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
	
	/**
	 * 某一消费的资金流水
	 * @param consumeLogID
	 * @return
	 * @throws SQLException
	 */
	public List<FundLog> getFundLogListByConsumeLogID(int consumeLogID) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from t_yw_ftfundlog where nConsumeLogID=? order by nUserID";

		List<FundLog> result = null;
		try {

			Object[] objs = new Object[] { consumeLogID };

			result = (List<FundLog>) this.query(conn, sql, new BeanListHandler<FundLog>(
					FundLog.class), objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
	
	
	public void removeFundLogs(int consumeLogID) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "delete from t_yw_ftfundlog where nConsumeLogID=?";

		try {

			Object[] objs = new Object[] { consumeLogID };

			this.update(conn, sql, objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}
	}
	
	
	public List<FundLog> getFundLogListByGroupID(int groupID, int userID) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "select * from t_yw_ftfundlog where nGroupID=? and nUserID=? order by nUserID";

		List<FundLog> result = null;
		try {

			Object[] objs = new Object[] { groupID,userID };

			result = (List<FundLog>) this.query(conn, sql, new BeanListHandler<FundLog>(
					FundLog.class), objs);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
	
	
	public long addFundLog(FundLog fundLog) throws SQLException {

		Connection conn = dataSource.getConnection();

		String sql = "insert into t_yw_ftfundlog(nConsumeLogID,nGroupID,nUserID,nMoney,dUpdateTime) values(?,?,?,?,?)";

		long result = -1;
		try {

			Object[] objs = new Object[] { 
					fundLog.getnConsumeLogID(),
					fundLog.getnGroupID(),
					fundLog.getnUserID(),
					fundLog.getnMoney(),
					fundLog.getdUpdateTime()};

			this.update(conn, sql, objs);
			result = this.getLastInsertID(conn);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtils.close(conn);
		}

		return result;
	}
}
